package com.jdk.learn.thread;

/**
 * synchronized 默认锁的是当前对象，在同一个线程中当前对象是可重入的，即结果是交替执行
 * 等价于ReentrantLock
 */
public class LockSynchronizeDemoCopy implements Runnable {
    @Override
    public void run() {
        get();
    }

    public void get() {
        synchronized (this) {
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("name:" + Thread.currentThread().getName() + ",get");
            set();
        }
    }

    public  void set() {
        synchronized (this) {
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("name:" + Thread.currentThread().getName() + ",set");
    }

    public static void main(String[] args) {
        LockSynchronizeDemoCopy lockSynchronizeDemo = new LockSynchronizeDemoCopy();
        new Thread(lockSynchronizeDemo, "1").start();
        new Thread(lockSynchronizeDemo, "2").start();
        new Thread(lockSynchronizeDemo, "3").start();
        new Thread(lockSynchronizeDemo, "4").start();
    }
}
